<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="dictionarydetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;" max-height="350"  >
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>

      <el-table-column prop="name" label="种类名称" width="120">
      </el-table-column>
      <el-table-column prop="manager.name" label="品牌名称" width="120">
      </el-table-column>

      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize"
                     :current-page="currentPage" :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典明细名称" prop="sn">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>

<!--        <el-form-item label="字典明细类型">-->
<!--          &lt;!&ndash; <el-input v-model="editForm.manager_id" auto-complete="off"></el-input>&ndash;&gt;-->
<!--          <template>-->
<!--            <el-select v-model="editForm.types_id" placeholder="请选择">-->
<!--              <el-option-->
<!--                  v-for="item in managers"-->
<!--                  :label="item.name"-->
<!--                  :value="item.id">-->
<!--                <span style="float: left">{{ item.name }}</span>-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </template>-->
<!--        </el-form-item>-->
        <el-form-item label="字典明细类型">
        <template>
          <el-select v-model="editForm.types_id" placeholder="请选择">
            <el-option
                v-for="item in managers"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </template>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>

    </el-dialog>



  </section>
</template>

<script>

export default {
  data() {
    return {
      filters: {
        name: ''
      },
      dictionarydetails: [],
      totals: 0,
      currentPage: 1,
      pageSize: 3,
      listLoading: false,
      sels: [],//列表选中列



      keyword: '',
      //修改或添加的对话框名称
      title: '',

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ]
      },
      //修改和添加的模型数据
      editForm: {
        id: null,
        name: '',
        types_id:null,

      },
      //部门经理
      managers: '',


    }
  },
  methods: {

    //分页查询 - 点击分页栏触发方法
    handleCurrentChange(val) {
      this.currentPage = val;
      //获取列表
      this.getDictionarydetails();
    },
    //获取用户列表
    getDictionarydetails() {
      let paras = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      //显示加载框
      this.listLoading = true;

      this.$http.post("/dictionarydetail", paras).then(res => {
        //给totals赋值 - 计算分页栏
        this.totals = res.data.totals;
        //赋值分页数据 - 显示当前页的分页数据
        this.dictionarydetails = res.data.data;


        this.listLoading = false;
      }).catch(res => {
        this.$message.error("系统繁忙,请稍后再试!!!")
      })
    },

    //3.高级查询
    keywordQuery() {
      //将当前页置为1，防止出现查询错误
      this.currentPage = 1;
      this.getDictionarydetails();
    },


    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;//显示加载框
        this.$http.delete("/dictionarydetail/" + row.id).then(res => {
          //成功之后的处理
          if (res.data.success) {
            //判断删除的是否是最后一个,如果是最后一个,就让当前页减一,
            let totalPage = Math.ceil(this.totals / this.pageSize)
            if (this.currentPage == totalPage && (this.totals - 1) % this.pageSize == 0 && this.currentPage != 1) {
              this.currentPage = this.currentPage - 1;
            }

            this.getDictionarydetails()
          } else {
            this.$message.error("系统繁忙,请稍后重试!!【500】")
          }
          //关闭加载框
          this.listLoading = false;
        }).catch(res => {
          this.$message.error("系统繁忙,请稍后重试!!【400】[404]")
        })
      }).catch(() => {

      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;

        this.$http.patch("/dictionarydetail", ids).then(res => {
          if (res.data.success) {
            this.currentPage = 1;
            this.getDictionarydetails();
          } else {
            this.$message.error("系统繁忙【500】")
          }
          this.listLoading = false;

        }).catch(res => {
          this.$message.error("系统错误，请稍后再试！！！")
        })
      }).catch(() => {

      });
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "修改"
      this.getManager();
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);

    },

    //显示新增界面
    handleAdd: function () {
      this.title = "添加"
      this.getManager();


      this.listLoading = false
      this.editFormVisible = true;
      this.editForm = {
        id: null,
        name: '',
        types_id:null,
      };
    },
    //添加或修改提交数据
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.listLoading =true


            let para = Object.assign({}, this.saveForm);

            this.$http.put("/dictionarydetail", this.editForm).then(res => {

              if (res.data.success) {

                this.getDictionarydetails()
                this.editFormVisible = false;

              } else {
                this.$message.error("系统繁忙,请稍后重试!!【500】")
              }
              this.listLoading = false
            }).catch(res => {
              this.$message.error("系统繁忙,请稍后重试!!【400】[404]")
            })
          });
        }
      });
    },
    //获取部门经理
    getManager() {
      this.$http.get("/dictionarytype").then(res => {
        this.managers = res.data;


      })
    },

  },
  //页面一加载就执行,钩子方法
  mounted() {
    this.getDictionarydetails();
  }
}

</script>

<style scoped>

</style>